package com.back.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.back.entity.Movie;
import com.back.service.inte.MovieService;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class MovieController {

    @Autowired
    private MovieService movieService;

    @GetMapping("/movie/randomlist")
    // 随机获得电影列表
    public SaResult randomList(@RequestParam("size" ) int size) {
        try {
            return SaResult.ok().setData(movieService.selectRandomMovies(size));
        } catch (Exception e) {
            return SaResult.error(e.getLocalizedMessage());
        }
    }

    // 通过电影名模糊和类型查询电影，带分页
    @GetMapping("/movie")
    public SaResult getMovies(
            @RequestParam("name") String name,
            @RequestParam("type") List<String> types,
            @RequestParam("current") int current,
            @RequestParam("pagesize") int pagesize
    ) {
        try {
            Map<String,Object> map = new HashMap<>();
            map.put("movie_list", movieService.selectMovieByNameAndTypes(name, types, (current-1)*pagesize, pagesize));
            map.put("total", movieService.selectMovieByNameAndTypesTotal(name, types));
            return SaResult.ok().setData(map);
        } catch (Exception e) {
            return SaResult.error(e.getLocalizedMessage());
        }
    }

    // 查看电影详情
    @GetMapping("/movie/getone")
    public SaResult getOne(@RequestParam("movie_id") int movie_id) {
        try {
            Map<String, Object> map = movieService.selectMovieById(movie_id);
            return SaResult.ok().setData(map);
        } catch (Exception e) {
            return SaResult.error().setMsg(e.getLocalizedMessage());
        }
    }

    @GetMapping("/type")
    public SaResult type() {
        Map<String, Object> map =new HashMap<>();
        map.put("type_list", movieService.selectMovieTypes());
        return SaResult.ok().setData(map);
    }

    // 添加电影
    // multipart/form-data
    @PostMapping("/movie")
    public SaResult addMovie(
            @RequestParam("name") String name,
            @RequestParam("type") String type,
            @RequestParam("description") String description,
            @RequestParam("nation") String nation,
            @RequestParam("date") String date,
            @RequestParam("img") MultipartFile img
    ) {
        try {
            try {
                StpUtil.checkLogin();
            }catch (Exception e) {
                return SaResult.error().setMsg("请先登录").setCode(401);
            }

            Movie movie = new Movie(name, type, description, nation, Timestamp.valueOf(date), null, 0, 0, 0);
            movieService.insertMovie(movie, img.getBytes(), img.getOriginalFilename());
            return SaResult.ok().setMsg("添加成功");
        }catch (Exception e) {
            return SaResult.error().setMsg(e.getLocalizedMessage());
        }
    }
}
